/* IMAGE UPLOAD */

function startUpload()
{
	document.getElementById('status').innerHTML = "Sending...";
	return true;
}

function stopUploadOK(msg)
{
	document.getElementById('status').innerHTML = "OK";
	showGallery();
	return true;
}
function stopUploadError(msg)
{
	document.getElementById('status').innerHTML = "Error";
	return true;
}
var log = false;
function showLog ()
{
	if(log)
		document.getElementById('form_target').style.display = "none";
	else
		document.getElementById('form_target').style.display = "block";
	log = !log;
}

/* GALLERY */

function showGallery ()
{
	dir = document.getElementById('galleryDir').value;
	g_request = createAjax( function(r)
	{
		document.getElementById('images').innerHTML = r;
	}, "gallery.php");
	g_request.send("dir="+dir);
}

function addImg (path, width, height)
{
	var adding = '<img src="'+path+'" width="'+width+'" height="'+height+'"> ';
	var ctrl = document.getElementById("editText");
	var startPos = ctrl.selectionStart;
	var endPos = ctrl.selectionEnd;
	caret = ctrl.selectionStart + adding.length;
	scrollTop = ctrl.scrollTop;
	scrollLeft = ctrl.scrollLeft;
	ctrl.value = ctrl.value.substring(0, startPos)+adding+ctrl.value.substring(endPos,ctrl.value.length);
	ctrl.focus();
	ctrl.setSelectionRange(caret,caret);
	ctrl.scrollTop = scrollTop;
	ctrl.scrollLeft = scrollLeft;
}

function addImgLink (path, width, height, dst)
{
	var adding = '<a href="'+dst+'"><img src="'+path+'" width="'+width+'" height="'+height+'"></a> ';
	var ctrl = document.getElementById("editText");
	var startPos = ctrl.selectionStart;
	var endPos = ctrl.selectionEnd;
	caret = ctrl.selectionStart + adding.length;
	scrollTop = ctrl.scrollTop;
	scrollLeft = ctrl.scrollLeft;
	ctrl.value = ctrl.value.substring(0, startPos)+adding+ctrl.value.substring(endPos,ctrl.value.length);
	ctrl.focus();
	ctrl.setSelectionRange(caret,caret);
	ctrl.scrollTop = scrollTop;
	ctrl.scrollLeft = scrollLeft;
}

function deleteImg (dir, file)
{
	if(confirm("Supprimer l'image ?"))
	{
		di_request = createAjax( function(r)
		{
			alert(r);
			showGallery();
		}, "action.php");
		di_request.send("deleteImage=1&dir="+textEncode(dir)+"&file="+textEncode(file));
	}
}

/* ADD POST & EDIT POST */

function addPost ()
{
	title = textEncode(document.getElementById('editTitle').value);
	if(window.tinyMCE) text = textEncode(tinyMCE.get('editText').getContent());
	else text = textEncode(document.getElementById('editText').value);
	date = textEncode(document.getElementById('editDate').value);
	a_request = createAjax( function(r)
	{
		alert(r);
		document.location.href = "index.php";
	}, "action.php");
	a_request.send("add=1&titre="+title+"&texte="+text+"&date="+date);
}

function editPost (id)
{
	title = textEncode(document.getElementById('editTitle').value);
	if(window.tinyMCE) text = textEncode(tinyMCE.get('editText').getContent());
	else text = textEncode(document.getElementById('editText').value);
	date = textEncode(document.getElementById('editDate').value);
	e_request = createAjax( function(r)
	{
		alert(r);
		document.location.href = "index.php?id="+id;
	}, "action.php");
	e_request.send("edit="+id+"&titre="+title+"&texte="+text+"&date="+date);
}

var previewWindow = null;
function previewPost ()
{
	if(previewWindow==null || previewWindow.closed)
		previewWindow = window.open ('../?preview&post=1', 'preview');
	setTimeout(previewPostLoad,1000);
}
function previewPostLoad ()
{
	title = document.getElementById('editTitle').value;
	if(window.tinyMCE) text = textEncode(tinyMCE.get('editText').getContent());
	else text = document.getElementById('editText').value;
	date = document.getElementById('editDate').value;
	//$dateString = date('d/m/Y, H:i',strtotime($_GET['date']));
	previewWindow.document.getElementById("previewTitle").innerHTML = title;
	previewWindow.document.getElementById("previewText").innerHTML = text;
	previewWindow.document.getElementById("previewDate").innerHTML = date;
	//previewWindow.focus();
}

function deletePost (id)
{
	if(confirm('Effacer ?'))
	{
		d_request = createAjax( function(r)
		{
			alert(r);
			document.location.href = "index.php";
		}, "action.php");
		d_request.send("delete="+id);
	}
}

/* DRAFTS */

function saveDraft ()
{
	title = textEncode(document.getElementById('editTitle').value);
	text = textEncode(document.getElementById('editText').value);
	draft = textEncode(document.getElementById('draft').value);
	if(draft=="") draft = prompt("Nom du brouillon", "draft"+Math.round(Math.random(0)*100));
	if(draft==null) return;
	sdraft_request = createAjax( function(r)
	{
		alert(r);
	}, "action.php");
	sdraft_request.send("draft="+draft+"&titre="+title+"&texte="+text);
}

function listDraft ()
{
	ldraft_request = createAjax( function(r)
	{
		document.getElementById("draftlist").innerHTML = r;
	}, "action.php");
	ldraft_request.send("listdraft=1");
}

function loadDraft (file)
{
	getAjax( function(r)
	{
		t = r.split("\n");
		document.getElementById("editTitle").value = t[0];
		document.getElementById("editText").value = "";
		for(i=1; i<t.length; i++)
			document.getElementById("editText").value += t[i]+"\n";
		document.getElementById('draft').value = file;
	}, "draft/"+file+".txt"+"?x=" + new Date().getTime());
}

function deleteDraft (file)
{
	if(confirm("Supprimer le brouillon ?"))
	{
		ddraft_request = createAjax( function(r)
		{
			alert(r);
			listDraft();
		}, "action.php");
		ddraft_request.send("deletedraft="+file);
	}
}

/* EDIT & DELETE COMM */

function editComm (id)
{
	name = textEncode(document.getElementById('editName').value);
	email = textEncode(document.getElementById('editMail').value);
	web = textEncode(document.getElementById('editWeb').value);
	text = textEncode(document.getElementById('editText').value);
	ec_request = createAjax( function(r)
	{
		alert(r);
		document.location.href = "commentaire.php?id="+id;
	}, "action.php");
	ec_request.send("editComm="+id+"&nom="+name+"&texte="+text+"&email="+email+"&web="+web);
}

function deleteComm (id)
{
	if(confirm('Effacer le commentaire ?'))
	{
		dc_request = createAjax( function(r)
		{
			alert(r);
		}, "action.php");
		dc_request.send("deleteComm="+id);
	}
}

